<?php

namespace App\Http\Controllers;

use App\Http\Logic\Admin;
use App\Http\Logic\Login;
use App\Models\AdminUser;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Logic\Admin as AdminLogic;

class LoginController extends Controller
{
    /**
     * 登录
     * @param Request $request
     * @return JsonResponse
     */
    public function login(Request $request): JsonResponse
    {
        // 校验数据
        try {
            $data = $request->validate([
                'account' => ['required', 'string'],
                'password' => ['required', 'string'],
            ]);

            // 校验通过，获取用户
            $user = AdminUser::query()->select(['id'])
                ->where('nickname', $data['account'])
                ->where('pwd', md5($data['password']))
                ->first();

            if (!$user) {
                return $this->retError(10002, "帐户或密码错误!");
            }

            $user = $user->toArray();

            // 获取用户登录token
            $logic = Login::getInstance();
            $token = $logic->createToken($user['id']);

            return $this->retSuccess(['token' => $token]);
        } catch (Exception $e) {
            return $this->retError(-1, $e->getMessage());
        }
    }

    /**
     * 个人信息
     * @param Request $request
     * @return JsonResponse
     */
    public function self(Request $request): JsonResponse
    {
        return $this->retSuccess(AdminLogic::getInstance()->info($this->getUid($request)));
    }

    /**
     * 用户菜单用于权限控制
     * @param Request $request
     * @return JsonResponse
     */
    public function menu(Request $request): JsonResponse
    {
        return $this->retSuccess(AdminLogic::getInstance()->getUserMenu($this->getUid($request)));
    }

    /**
     * 设置分组用户
     * @param Request $request
     * @return JsonResponse
     */
    public function setGroupUser(Request $request): JsonResponse
    {
        // 校验数据
        try {
            $data = $request->validate([
                'id' => ['required', 'int', 'min:1'],
                'user_ids' => ['array'],
            ]);

            Admin::getInstance()->setGroupUser($data['id'], $data['user_ids']);
            return $this->retSuccess();
        } catch (Exception $e) {
            return $this->retError(-1, $e->getMessage());
        }
    }

    /**
     * 设置分组权限
     * @param Request $request
     * @return JsonResponse
     */
    public function setGroupMenu(Request $request): JsonResponse
    {
        // 校验数据
        try {
            $data = $request->validate([
                'id' => ['required', 'int', 'min:0'],
                'menu_ids' => ['array'],
            ]);

            Admin::getInstance()->setGroupMenu($data['id'], $data['menu_ids']);
            return $this->retSuccess();
        } catch (Exception $e) {
            return $this->retError(-1, $e->getMessage());
        }
    }

    /**
     * 分组列表
     * @param Request $request
     * @return JsonResponse
     */
    public function groupList(Request $request): JsonResponse
    {
        $params['size'] = $request->input('size', 10);
        $params['page'] = $request->input('page', 1);
        $list = Admin::getInstance()->groupList($params);
        return $this->retSuccess($list);
    }
}
